/*
03-树2 List Leaves
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../base/Tree-Traversal-Level.c"
// #include "../base/Tree-Traversal-Recursive.c"


/* current ===============================*/
int isp = 0;
void visitTreeNode(TreeNode* node) {
    // printf("%d ", node->data);
    if (node->left == NULL && node->right == NULL) {
        if (isp) {
            printf(" ");
        }
        printf("%d", node->data);
        isp = 1;
    }
}

Tree read(TreeNode* nodes) {
    int n;
    scanf("%d", &n);
    // 是否叶子节点
    int *isLeave = (int*) malloc(sizeof(int)*n);
    memset(isLeave, 0, sizeof(int)*n);
    for (int i=0; i<n; i++) {
        char slidx[5], sridx[5];
        scanf("%s %s", slidx, sridx);
        char lidx = slidx[0];
        char ridx = sridx[0];
        nodes[i].data = i;
        // printf("%d) %d %s %s\n", i, nodes[i].data, lidx, ridx);
        if (lidx == '-') {
            nodes[i].left = NULL;
        } else {
            nodes[i].left = &nodes[lidx-'0'];
            isLeave[lidx-'0'] = 1;
        }
        if (ridx == '-') {
            nodes[i].right = NULL;
        } else {
            nodes[i].right = &nodes[ridx-'0'];
            isLeave[ridx-'0'] = 1;
        }
    }
    Tree root = NULL;
    for (int i=0 ;i<n; i++) {
        if (!isLeave[i]) {
            root = &nodes[i];
            break;
        }
    }
    free(isLeave);
    return root;
}

#define MAX_SIZE 10
int main() {
    // 预先分配t1, t2的内存空间
    TreeNode t1nodes[MAX_SIZE];
    Tree tree = read(t1nodes);
    // preorderTraversal(tree);
    // printf("\n");
    LevelTraversal(tree);
    printf("\n");
    return 0;
}
